package com.example.dwb_homework3;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView tvEventInfo;
    Button btnInnerClass;
    Button btnAnonymous;
    Button btnActivityImpl;
    Button btnExternal;
    Button btnXml;
    Button btnSystemInfo;
    Button btnProgress;
    TextView tvSystemInfo;

    // 进度对话框相关
    private static final int PROGRESS_DIALOG = 0x112;
    private int[] data = new int[100];
    private int hasData = 0;
    private int progressStatus = 0;
    private ProgressDialog pd;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvEventInfo = findViewById(R.id.tv_event_info);
        btnInnerClass = findViewById(R.id.btn_inner_class);
        btnAnonymous = findViewById(R.id.btn_anonymous);
        btnActivityImpl = findViewById(R.id.btn_activity_impl);
        btnExternal = findViewById(R.id.btn_external);
        btnXml = findViewById(R.id.btn_xml);
        btnSystemInfo = findViewById(R.id.btn_system_info);
        btnProgress = findViewById(R.id.btn_progress);
        tvSystemInfo = findViewById(R.id.tv_system_info);

       btnInnerClass.setOnClickListener(new ExternalClickListener(tvEventInfo));
       btnAnonymous.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               tvEventInfo.setText("事件处理方式: 匿名内部类");
           }
       });
       btnActivityImpl.setOnClickListener(this);
       btnExternal.setOnClickListener(new Exterior(tvEventInfo));

       btnSystemInfo.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               showSystemInfo();
           }
       });

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 0x123) {
                    // 更新进度
                    pd.setProgress(progressStatus);
                }
                return false;
            }
        });

        btnProgress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressStatus = 0;
                hasData = 0;
                showDialog(PROGRESS_DIALOG);
            }
        });
    }

    // XML绑定方式
    public void onXmlClick(View v) {
        tvEventInfo.setText("事件处理方式: XML绑定");
    }

    // 获取系统信息
    public void showSystemInfo() {
        Configuration cfg = getResources().getConfiguration();

        String screen = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE ?
                "横向屏幕" : "竖向屏幕";

        String naviName;
        switch (cfg.navigation) {
            case Configuration.NAVIGATION_NONAV:
                naviName = "没有方向控制";
                break;
            case Configuration.NAVIGATION_WHEEL:
                naviName = "滚轮控制方向";
                break;
            case Configuration.NAVIGATION_DPAD:
                naviName = "方向键控制方向";
                break;
            case Configuration.NAVIGATION_TRACKBALL:
                naviName = "轨迹球控制方向";
                break;
            default:
                naviName = "未知控制方式";
        }

        String touchName;
        switch (cfg.touchscreen) {
            case Configuration.TOUCHSCREEN_NOTOUCH:
                touchName = "无触摸屏";
                break;
            case Configuration.TOUCHSCREEN_STYLUS:
                touchName = "触摸笔式触摸屏";
                break;
            case Configuration.TOUCHSCREEN_FINGER:
                touchName = "接受手指的触摸屏";
                break;
            default:
                touchName = "未知触摸屏类型";
        }

        String info = String.format(
                "屏幕方向: %s\n" +
                        "导航方式: %s\n" +
                        "触摸屏类型: %s\n" +
                        "移动网络代码: %d",
                screen, naviName, touchName, cfg.mnc
        );

        tvSystemInfo.setText(info);
    }

    @Nullable
    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
        switch (id) {
            case PROGRESS_DIALOG:
                // 创建进度对话框
                pd = new ProgressDialog(this);
                pd.setMax(100);
                // 设置对话框的标题
                pd.setTitle("任务完成百分比");
                // 设置对话框 显示的内容
                pd.setMessage("耗时任务的完成百分比");
                // 设置对话框不能用“取消”按钮关闭
                pd.setCancelable(false);
                // 设置对话框的进度条风格
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                // 设置对话框的进度条是否显示进度
                pd.setIndeterminate(false);
                break;
        }
        return pd;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);

        if (id == PROGRESS_DIALOG) {
            // 重置进度
            pd.setProgress(0);

            // 启动后台线程模拟耗时操作
            new Thread(() -> {
                while (progressStatus < 100) {
                    progressStatus = doWork();
                    // 发送消息更新UI
                    handler.sendEmptyMessage(0x123);
                }
                // 任务完成，关闭对话框
                if (progressStatus >= 100) {
                    pd.dismiss();
                    runOnUiThread(() ->
                            Toast.makeText(MainActivity.this, "任务完成!", Toast.LENGTH_SHORT).show()
                    );
                }
            }).start();
        }
    }

    // 模拟耗时操作
    private int doWork() {
        // 填充数组
        if (hasData < data.length) {
            data[hasData++] = (int) (Math.random() * 100);
        }

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return hasData;
    }

    @Override
    public void onClick(View view) {
        tvEventInfo.setText("事件处理方式: Activity实现接口");
    }

    static class ExternalClickListener implements View.OnClickListener {
        private final TextView tvEventInfo;

        public ExternalClickListener(TextView tvEventInfo) {
            this.tvEventInfo = tvEventInfo;
        }

        @Override
        public void onClick(View v) {
            tvEventInfo.setText("事件处理方式: 内部类");
        }
    }
}